#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>

using namespace std;

class Solution
{
public:
    int left, right, n;
    string path;
    vector<string> ret;
    vector<string> generateParenthesis(int _n)
    {
        n = _n;
        dfs();
        return ret;
    }

    void dfs()
    {
        if (path.size() == 2 * n)
        {
            ret.push_back(path);
            return;
        }

        if (left < n)
        {
            left++;
            path.push_back('(');
            dfs();
            path.pop_back();
            left--;
        }
        if (right < left)
        {
            right++;
            path.push_back(')');
            dfs();
            path.pop_back();
            right--;
        }

    }
};